package com.easyadmin.modules.sys.controller;

import com.easyadmin.common.context.UserContext;
import com.easyadmin.modules.sys.domain.SysUser;
import com.github.pagehelper.PageInfo;
import com.easyadmin.modules.sys.domain.SysTextArea;
import com.easyadmin.common.web.base.BaseController;
import com.easyadmin.common.web.domain.request.PageDomain;
import com.easyadmin.common.web.domain.response.Result;
import com.easyadmin.common.web.domain.response.module.ResultTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.easyadmin.modules.sys.service.ISysTextAreaService;

import java.util.Arrays;
import java.util.List;

/**
 * 富文本存储Controller
 *
 * @author lee
 * @date 2025-10-24
 */
@RestController
@RequestMapping("/sys/textArea")
public class SysTextAreaController extends BaseController {

    private String prefix = "sys/textArea";

    @Autowired
    private ISysTextAreaService sysTextAreaService;

    @GetMapping("/index")
    //@PreAuthorize("hasPermission('/sys/textArea/main','sys:textArea:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/index");
    }

    /**
     * 查询富文本存储列表
     */
    @ResponseBody
    @GetMapping("/data")
//    @PreAuthorize("hasPermission('/sys/textArea/data','sys:textArea:data')")
    public ResultTable list(@ModelAttribute SysTextArea sysTextArea, PageDomain pageDomain) {
        PageInfo<SysTextArea> pageInfo = sysTextAreaService.selectSysTextAreaPage(sysTextArea, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    @ResponseBody
    @GetMapping("/list")
//    @PreAuthorize("hasPermission('/sys/textArea/data','sys:textArea:data')")
    public Result list() {
        SysUser sysUser = UserContext.currentUser();
        SysTextArea sysTextArea = new SysTextArea();
        sysTextArea.setUserId(Long.parseLong(sysUser.getUserId()));
        List<SysTextArea> lists = sysTextAreaService.selectSysTextAreaList(sysTextArea);
        return success(lists);
    }

    @ResponseBody
    @GetMapping("/get/{id}")
//    @PreAuthorize("hasPermission('/sys/textArea/data','sys:textArea:data')")
    public Result get(@PathVariable Long id) {

        SysTextArea sysTextArea = sysTextAreaService.getById(id);
        return success(sysTextArea);
    }

    /**
     * 新增富文本存储
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/sys/textArea/add','sys:textArea:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增富文本存储
     */
    @ResponseBody
    @PostMapping("/save")
//    @PreAuthorize("hasPermission('/sys/textArea/add','sys:textArea:add')")
    public Result save(@RequestBody SysTextArea sysTextArea) {
        SysUser sysUser = UserContext.currentUser();
        sysTextArea.setUserId(Long.parseLong(sysUser.getUserId()));
        sysTextArea.setCreateBy(sysUser.getUsername());
        sysTextArea.setUpdateBy(sysUser.getUsername());
        sysTextAreaService.save(sysTextArea);
        return success(sysTextArea.getId());
    }

    /**
     * 修改富文本存储
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/sys/textArea/edit','sys:textArea:edit')")
    public ModelAndView edit(Long id, ModelMap map) {
        SysTextArea sysTextArea =sysTextAreaService.getById(id);
        map.put("sysTextArea", sysTextArea);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改富文本存储
     */
    @ResponseBody
    @PutMapping("/update")
//    @PreAuthorize("hasPermission('/sys/textArea/edit','sys:textArea:edit')")
    public Result update(@RequestBody SysTextArea sysTextArea) {
        return decide(sysTextAreaService.updateById(sysTextArea));
    }

    /**
     * 删除富文本存储
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
//    @PreAuthorize("hasPermission('/sys/textArea/remove','sys:textArea:remove')")
    public Result batchRemove(String ids) {
        return decide(sysTextAreaService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除富文本存储
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
//    @PreAuthorize("hasPermission('/sys/textArea/remove','sys:textArea:remove')")
    public Result remove(@PathVariable("id") Long id) {
        return decide(sysTextAreaService.removeById(id));
    }
}
